package com.lzm.back.controller;

import com.github.pagehelper.PageInfo;
import com.lzm.back.common.MyResult;
import com.lzm.back.entity.User;
import com.lzm.back.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 新增参赛员
     *
     * @param newUser
     * @return
     */

    @PostMapping("/addUser")
    public MyResult addUser(@RequestBody User newUser) {
        System.out.println(newUser);
        userService.addUser(newUser);
        return MyResult.success(newUser);

    }

    /**
     * 更新参赛人员
     *
     * @param newUser
     * @return
     */
    @PutMapping("/updateUser")
    public MyResult updateUser(@RequestBody User newUser) {
        userService.updateUser(newUser);
        return MyResult.success(newUser);
    }

    /**
     * 删除单个数据
     *
     * @param id
     * @return
     */

    @DeleteMapping("/deleteUserById/{id}")
    public MyResult deleteUserById(@PathVariable Integer id) {
        userService.deleteUserById(id);
        return MyResult.success();
    }


    /**
     * 批量删除
     */
    @DeleteMapping("/deleteUserByIdBatch")
    public MyResult deleteUserByIdBatch(@RequestBody List<Integer> ids) {//body 原因 传进来数组
        userService.deleteUserByIdBatch(ids);
        return MyResult.success();
    }

    /**
     * 查询所有user数据
     *
     * @return
     */
    @GetMapping("/selectAll")
    public MyResult selectAll(User User, String role) {
        List<com.lzm.back.entity.User> userServiceList = userService.selectAll(User, role);
        return MyResult.success(userServiceList);
    }

    /**
     * 查询 selectByID/id=
     *
     * @param id
     * @return
     */

    @GetMapping("/selectByID/{id}")
    public MyResult selectByID(@PathVariable Integer id) {
        User User = userService.selectByID(id);
        return MyResult.success(User);
    }

    /**
     * url
     *
     * @param id
     * @return
     */
    @GetMapping("/selectOne")
    public MyResult selectOne(@RequestParam Integer id) {
        User User = userService.selectByID(id);
        return MyResult.success(User);
    }

    /**
     * 分页查询
     * url请求： ?pagenum=&pagesize=//
     *
     * @param pageNum
     * @param pageSize 每页个数
     * @return
     */

    @GetMapping("/selectPage")
    public MyResult selectPage(User User,
                               @RequestParam(required = false) String username,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               @RequestParam String role) {
        PageInfo<com.lzm.back.entity.User> userPageInfo = userService.selectPage(User, username, pageNum, pageSize, role);

        return MyResult.success(userPageInfo);
    }

}
